/* 
 * File:   qt_serialize.h
 * Author: Arnaud Bellec
 *
 * Created on April 9, 2011, 2:42 PM
 */

#ifndef QT_SERIALIZE_H
#define	QT_SERIALIZE_H

#include <boost/serialization/split_free.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/string.hpp>

#include <QList>
#include <QString>

BOOST_SERIALIZATION_SPLIT_FREE(QString)

#include <boost/serialization/nvp.hpp>
#define NVP(x) BOOST_SERIALIZATION_NVP(x)

namespace boost {
namespace serialization {

template<class Archive>
void save(Archive & ar, const QString & s, unsigned int version)
{
    std::string value = s.toStdString();
    ar << NVP(value);
}
template<class Archive>
void load(Archive & ar, QString & s, unsigned int version)
{
    std::string value;
    ar >> NVP(value);
    s.clear();
    s.append(QString::fromStdString(value));
}

template<class Archive, class T>
void save(Archive & ar, const QList<T> & list, unsigned int version)
{
    std::list<T> items = list.toStdList();
    ar << NVP(items);
}
template<class Archive, class T>
void load(Archive & ar, QList<T> & list, unsigned int version)
{
    std::list<T> items;
    ar >> NVP(items);

    list.clear();
    list.append(QList<T>::fromStdList(items));
}
template<class Archive, class T>
void serialize(Archive & ar, QList<T> &t, const unsigned int file_version){
    boost::serialization::split_free(ar, t, file_version);
}

} // namespace serialization
} // namespace boost

#endif	/* QT_SERIALIZE_H */

